﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using MyERP.Models;

namespace MyERP.WebApi
{
    [Route("api/[controller]/[Action]")]
    [ApiController]
    [Authorize(Roles = "admin")]
    public class UsersController : ControllerBase
    {
        private readonly MyDbContext db;
        public UsersController(MyDbContext db) 
        {
            this.db = db;
        }

        [HttpGet]
        public ActionResult getUserList() 
        {
            List<UserList> result = db.Users
                .Select( x => new UserList
                {
                    Name = x.Name,
                    PhoneNumber = x.PhoneNumber,
                    q1 = x.q1,
                    q2 = x.q2,
                    q3 = x.q3,
                    q4 = x.q4
                })
                .ToList();
            return Ok(new {code=0,msg="",count=result.Count,data=result});
        }
        /// <summary>
        /// 获取用户信息
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult<Users> GetUserOne(string name)
        {
            Users? user = db.Users.FirstOrDefault(x => x.Name == name);
            if (user == null)
            {
                return Ok(new { code = 404, message = "用户不存在"});
            }

            return Ok(new {code = 200, data = user });
        }

        [HttpPost]
        public ActionResult Add([FromBody] Users user)
        {
            Users? u = db.Users.FirstOrDefault(x => x.Name == user.Name);
            if (u != null)
            {
                return Ok(new {code = 404, message = "用户名已存在"});
            }
            db.Users.Add(user);
            db.SaveChanges();
            return Ok(new { code = 200, message = "用户添加成功" });
        }

        [HttpPost]
        public ActionResult Edit([FromBody] Users user)
        {
            Users? u = db.Users.FirstOrDefault(x => x.Id == user.Id);
            if (u == null)
            {
                return Ok(new { code = 404, message = "用户不存在" });
            }
            u.Name = user.Name;
            u.PhoneNumber = user.PhoneNumber;
            u.Password = user.Password;
            u.q1 = user.q1;
            u.q2 = user.q2;
            u.q3 = user.q3;
            u.q4 = user.q4;
            db.SaveChanges();
            return Ok(new { code = 200, message = "用户信息修改成功" });
        }

        [HttpGet]
        public ActionResult Del(string name)
        {
            Users? user = db.Users.FirstOrDefault(x => x.Name == name);
            if (user == null)
            {
                return Ok(new { message = $"{name} 不存在" });
            }
            db.Remove(user);
            db.SaveChanges();
            return Ok(new { message = $"{name} 删除成功" });
        }
    }

    public class UserList
    {
        public string Name { get; set; }
        public string PhoneNumber { get; set; }
        public int q1 { get; set; }
        public int q2 { get; set; }
        public int q3 { get; set; }
        public int q4 { get; set; }
    }
}
